/*
 * Copyright (C) 2008-2009 the original author or authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.code.liquidform;

import java.util.Collection;

import com.google.code.liquidform.internal.AliasFactory;
import com.google.code.liquidform.internal.managedobjects.FrameworkManagedObject;

/**
 * Represents the <tt>SELECT x, y, z</tt> part of a query. Selected elements can
 * be persistent entities or properties thereof. Selected elements can be
 * referenced using their alias, or directly <i>via</i> a getter method call
 * like such :
 * 
 * <pre>
 * final Person p = LiquidForm.use(Person.class, &quot;p&quot;);
 * select(p.getFirstName()).from(Person.class).as(p); // etc
 * </pre>
 * 
 */
// Previous clause is null anyways
public class SelectClause<S> extends AbstractClause {

	private final Object[] selectedElements;

	/* default */SelectClause(Object[] selectedElements) {
		super(null);
		this.selectedElements = selectedElements;
	}

	/**
	 * Express an identification variable declaration to a single valued
	 * property.
	 * 
	 * @return a stub that may be later used to restrict the query
	 */
	public <T> FromClause<T, S> from(T singleValued) {
		return new SingleValuedFromClause<T, S>(this, singleValued);
	}

	/**
	 * Express an identification variable declaration to a member of a
	 * collection.
	 * 
	 * @return a stub that may be later used to restrict the query
	 */
	public <T> FromClause<T, S> from(Collection<T> collectionValued) {
		return new CollectionValuedFromClause<T, S>(this, collectionValued);
	}

	/**
	 * Express an identification variable declaration to an abstract schema
	 * type.
	 * 
	 * @return a stub that may be later used to restrict the query
	 */
	public <T> FromClause<T, S> from(Class<T> type) {
		return new AbstractSchemaTypeFromClause<T, S>(this, type);
	}

	/**
	 * Express an identification variable declaration to a member of a
	 * collection.
	 * 
	 * @return a stub that may be later used to restrict the query
	 */
	public <T> FromClause<T, S> from(InCollection<T> inCollection) {
		return new InCollectionFromClause<T, S>(this, inCollection);
	}

	@Override
	/* default */StringBuilder addTo(StringBuilder buffer) {
		buffer.append("select ");
		for (int i = 0; i < selectedElements.length; i++) {
			FrameworkManagedObject fmo = AliasFactory
					.about(selectedElements[i]);

			buffer.append(fmo.getPreferredStringRepresentation());

			if (i < selectedElements.length - 1) {
				buffer.append(", ");
			}
		}
		return buffer;
	}

}
